package org.hackystat.devcathlon.ui.page.scoreboard.eventspanel;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.event.EventScore;
import org.hackystat.devcathlon.engine.match.Match;

/**
 * Display invoked Events in the Match Events panel.
 * 
 * @author Daniel Arakaki
 */
public class EventsPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Set up the listing of the Events.
   * 
   * @param id The Wicket id from the html file.
   * @param match The chosen match.
   */
  public EventsPanel(String id, Match match) {
    super(id);
    
    add(new Label("matchNameHeader", match.getName()));
    
    String noEvents = "";
    
    List<EventScore> scoresList = new ArrayList<EventScore>();
    if (match.getEventScores().isEmpty()) {
      noEvents = "";
    }

    for (EventScore s : match.getEventScores()) {
      scoresList.add(s);
    }

    DataView scoreView = new DataView("scoreList", new ListDataProvider(scoresList)) {
      
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(Item item) {
        EventScore scoreItem = (EventScore) item.getModelObject();

        
        try {
          item.add(new Label("date",  scoreItem.getTimestampDate().toString()));
        }
        catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        item.add(new Label("teamName", scoreItem.getTeam().getName()));
        item.add(new Label("points", String.valueOf(scoreItem.getBaseScore())));
        item.add(new Label("explantion", scoreItem.getExplanation()));
      }

    };
    scoreView.setItemsPerPage(3);
    add(scoreView);
    add(new PagingNavigator("navigator", scoreView));
    add(new Label("noEvents", noEvents));
  }
  
}
